package cn.itlaobing.springmvc.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.itlaobing.springmvc.entity.Yike;
import cn.itlaobing.springmvc.service.YikeService;

@Controller
@RequestMapping("/yikes")
public class YikeController extends BaseController{

	@Autowired
	private YikeService yikeService;
	//查看
	@GetMapping
	public String index(Model model){
		
		model.addAttribute("yikes",yikeService.findBySearchFilter(parameters()));
		return "student/index";
	}
	
	@GetMapping("/create")
	public String _new(Model model){
		
		Yike yike=new Yike();
		model.addAttribute("yike",yike);
		return "student/create";
	}
	@GetMapping("/yyy")
	public String search(Model model){
		
		model.addAttribute("yikes",yikeService.findBySearchFilter(parameters()));
		return "student/search";
	}
	@GetMapping("/ccc")
	public String changePwd(Model model){
		
		model.addAttribute("yikes",yikeService.findBySearchFilter(parameters()));
		return "student/changepwd";
	}
	@GetMapping("/sss")
	public String tree(Model model){
		
		model.addAttribute("yikes",yikeService.findAll());
		return "student/tree";
	}
	//保存或添加
	@PostMapping("/create")
	public String create(@Valid @ModelAttribute Yike yike,
			BindingResult bindResult,
			Model model,
			RedirectAttributes redirectAttributes){
		
		if(bindResult.hasErrors()){
			return "student/create";
		}
		
		yikeService.save(yike);
		/**
		 * 这个属性只保存一次
		 */
		redirectAttributes.addFlashAttribute("toastMsg", "保存"+yike.getArea()+"成功！");
		
		return "redirect:/yikes";
	}
	
	//修改前获取数据
	@GetMapping("/{id}/update")
	public String endit(@PathVariable Integer id, Model model){
		
		Yike yike=yikeService.findById(id);
		model.addAttribute("yike",yike);
		
		return "student/edit";
	}
	//修改之后保存到数据库
	@PostMapping("/{id}/update")
	public String update(@PathVariable Integer id,
			@Valid @ModelAttribute Yike yike,
			BindingResult bindResult,
			Model model,
			RedirectAttributes redirectAttributes){
		
		if(bindResult.hasErrors()){
			return "student/edit";
		}
	
		yike.setId(id);
		yikeService.update(yike);
		redirectAttributes.addFlashAttribute("toastMsg", "更新"+yike.getArea()+"成功！");
		
		return "redirect:/yikes";
	}
	//删除
	@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
	@ResponseBody
	public Yike destory(@PathVariable Integer id){
		Yike yike=yikeService.findById(id);
		yikeService.delete(id);
		yike =  new Yike();
		yike.setArea("area");
		return yike;
	}
	/*@RequestMapping(value = "/ztree", method = RequestMethod.POST,produces = {"text/json;charset=UTF-8"})
	public @ResponseBody String destroy() {
		List<Yike> list = yikeService.findAll();
		List<String> lt = new ArrayList<String>();
		lt.add("{\"id\":\"1\",\"pId\":\"0\",\"name\":\"区域管理\",\"open\":\"true\"}");
		for(Yike yike :list){
			StringBuffer text = new StringBuffer();
			text.append("{\"id\":\""+yike.getId())
			.append("\",\"pId\":\"1\"")
			.append(",\"name\":"+"\""+yike.getArea()+"\"")
			.append("}");
			lt.add(text.toString());
			text.delete(0, text.length()-1);
		}
		log.info(lt.toString());
		return lt.toString();
	
	}*/

}
